<%@LANGUAGE="VBSCRIPT" %>

<% 
session("redir")=""
session("ticketsaved")=0
fromdispatch=request.QueryString("FromDispatch")
currentUser = Session("CurrentUser")

'----------------- status -------------------------
set db = server.CreateObject("adodb.connection")
set rs = server.CreateObject("adodb.recordset")
		  
filepath=server.MapPath("access/backend/DBHub.mdb")
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath

if fromdispatch="yes" then
	criteria="'dispatch','both'"
else
	criteria="'operator','both'"
end if
		
sql="select status_name, where from Admin_DispatchColors where type='status' order by status_name"
stats=""
statusoutput=""
stats=""
acceptablestatus=""
statusOptionClass = ""
		  
rs.cursorlocation=3
		  
rs.open sql, db, 1, 2
rs.activeconnection=nothing

'response.Write("criteria: " & criteria & "<br>")
do until rs.eof
	matchstring = "'" & lcase(rs("where")) &  "'"
	if instr(criteria, matchstring) > 0 then
		statusOptionClass = ""
		disabled = ""
		acceptablestatus=acceptablestatus & rs("status_name") & "|"
	else
		statusOptionClass = "NotSelectable"
		disabled = "disabled='disabled'"
	end if
	'response.Write ("matchstring: " & matchstring & " | instr: " & instr(criteria, matchstring) & "<br>")
	
	statusoutput=statusoutput & "<option value='" & rs("status_name") & "' class='" & statusOptionClass & "'" & disabled & ">" & rs("status_name") & "</option>" & vbcrlf
	stats=stats & rs("status_name") & "|"
	rs.movenext
loop
rs.close

stats=left(stats,len(stats)-1)

'------------------- end status --------------------

'------------------- begin tickets history-----------------
ticketNum = request.QueryString("Ticket_ID")

if ticketNum<>"" then
	sql="select * from Tkt_Ticket_History where Ticket_ID=" & ticketnum
	rs.open sql, db, 1, 2
	
	if not rs.eof then
		x=0
		ra=rs.getrows() '0=Note_ID, 1=Ticket_ID, 2=User, 3=When, 4=Note
		rs.close
		rowcount=ubound(ra,2)
		do until x>rowcount
			notesoutput=notesoutput & "<tr><td bgcolor='#D2FFD2' class='TableTitles'>" & x+1 & ") " & ra(2,x) & " &nbsp; " & ra(3,x) & "</td><td bgcolor='#D2FFD2' class='TableTitles'>" & ra(4,x) & "</td></tr>" & vbcrlf
		x=x+1
		loop
		
	else
		rowcount=0
		notesoutput="<tr><td>(This Ticket Has No History)</td></tr>"
	end if
	'---------------- end ticket history -----------------------
	
	'---------------- begin ticket --------------------------
	sql="select * from Tkt_Tickets where ticket_id=" & ticketNum
	
	set trs=server.CreateObject("adodb.recordset")
	
	trs.open sql, db, 1, 2
	ra=trs.getrows() '0=Ticket_ID, 1=Contract_ID, 2=Status, 3=Given_To, 4=Created_Time, 5=Completed_Time, 6=Appliance, 7=Emergency, 8=follow up
end if
	'------------------ end ticket -----------------------
	'------------------ end ASP --------------------------	
%>	
	
<HTML XMLNS="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" />
<TITLE>Ticket Window</TITLE>
<LINK HREF="UniversalStyleSheet.css" REL="stylesheet" TYPE="text/css" />
<STYLE TYPE="text/css">
<!--
body {
	margin-left: 1px;
	margin-top: 1px;
	margin-right: 1px;
	margin-bottom: 1px;
}

-->
</STYLE>

<SCRIPT TYPE="text/javascript">

function FrmSubmit()
    {
	var CID=document.getElementById("Contract_ID").value;
	var Appl=document.getElementById("Appliance").selectedIndex;
	var Emerg=document.getElementById("Emergency").value;
	var Note=document.getElementById("Notes").value;
	var stat=document.getElementById("Status").selectedIndex;
	var msg="";
	<%
	if fromdispatch="yes" then
		%>
		var distime=document.getElementById("GivenToTime").value;
		<%
	end if
	%>

	var emer=document.getElementById("Emergency");
	var tf=document.getElementById("TicketForm");

	if (CID=="") {
		alert ("You Have Not Selected An Account To Save The Ticket To. \n If You've Already Pulled Up An Account Then Click 'New Ticket' To Associate the Contract ID With This Ticket.");
 		return;
	}//end if

	if (stat==-1 || stat==0) {msg += "- Status \n";}

	if (Appl==-1 || Appl==0){ msg += "- Appliance \n";}

	if (Emerg != "Yes" && Emerg != "No") {msg += "- Emergency \n";}

	if (Note=="") {msg += "- Notes \n";}
	

	if (msg != ""){
		alert ("Cant Save Changes. The Following Required Fields Were Blank:\n\n" + msg);
		return;
	}//end if

	tf.onsubmit ="";
	<% 
	if fromdispatch="yes" then 
		%>
		document.TicketForm.action="SaveTicket.asp?FromDispatch=yes&user=<%= currentUser %>";
		<% 
	else 
		randomize() 'this randomized number is to make the saveticket url unique so the IE bug doesnt run the code twice
		uniqueid=hex(100000000 * rnd)
		%>
		tf.action="SaveTicket.asp?uid=<%= uniqueid %>&user=<%= currentUser %>"
		<% 
	end if 
	%>
	tf.submit();
    }

function status_change(){
	var stat=document.getElementById("status");
	var oldstat=document.getElementById("statusPH");
	
	var acceptable=0;
	
	var accpt="<%= acceptablestatus %>";
	var accptarray=accpt.split("|");
	
	for (x=0;x< accptarray.length;x++)
	    {
		if (stat.value == accptarray[x]){acceptable=1; break;}
	    }
	
	if (acceptable == 0){
		alert("Setting this ticket to a status of '" + stat.value + "' is not allowed in this screen.");
		stat.selectedIndex=oldstat.value
		return false;
	}//end if
	
	oldstat.value=stat.selectedIndex
	
}//end function


</SCRIPT>

</HEAD>

<BODY>
<FORM ID="TicketForm" NAME="TicketForm" METHOD="post" ACTION="SaveTicket.asp" onSubmit="javascript: return false;">
<TABLE WIDTH="730" BORDER="0" CELLSPACING="0" CELLPADDING="0">
 <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperLeft.png"></TD>
    <TD WIDTH="690" HEIGHT="20" BACKGROUND="Images/TableUpperMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleLeft.png"></TD>
    <TD WIDTH="590" BGCOLOR="#FFFFFF">
      <TABLE WIDTH="690" BORDER="0" CELLPADDING="0" CELLSPACING="3" BGCOLOR="#FFFFFF">
        <TR>
          <TD WIDTH="250" BGCOLOR="#D2FFD2" CLASS="SearchFormText"><span class="formLabel">Contract ID</span>
             
            <INPUT NAME="Contract_ID_Display" TYPE="text" ID="Contract_ID_Display" SIZE="15" disabled="disabled"/>
             
            <INPUT NAME="Contract_ID" TYPE="hidden" ID="Contract_ID" />
         <BR /></TD>
          <TD WIDTH="316" BGCOLOR="#D2FFD2" CLASS="SearchFormText"><span class="formLabel">Ticket Number</span>
             
            <INPUT NAME="Ticket_ID_Display" TYPE="text" ID="Ticket_ID_Display" SIZE="15" disabled="disabled"/>
            <INPUT NAME="Ticket_ID" TYPE="hidden" ID="Ticket_ID" />
         </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText"><span class="formLabel">Appliance</span>
            
            <SELECT NAME="Appliance" ID="Appliance" TABINDEX="1">
				<OPTION selected="selected" class="Choose" value="">Choose...</OPTION>
				  <%
				  dim fs, tf, appliances
				  
				  appliances = "Choose...|"
				  
				  set fs=server.CreateObject("scripting.filesystemobject")
				  set tf=fs.opentextfile(server.MapPath("access/backend/appliances.txt"))
				  
				  do until tf.atendofstream
			  		appl = tf.readline
			  		response.Write("<option value='" & appl & "'>" & appl & "</option>" & vbcrlf)
					appliances = appliances & appl & "|"
				  loop
				  
				  appliances = left(appliances, len(appliances) - 1)
				  
				  tf.close
				  
				  set tf = nothing
				  set fs = nothing
				  %>	
			  
            </SELECT>
            </TD>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText"><SPAN CLASS="formLabel"># of Calls</SPAN>
            <INPUT NAME="CallNumber" TYPE="text" ID="CallNumber" SIZE="1" disabled="disabled"/>
            &nbsp; &nbsp;<SPAN CLASS="formLabel">Status</SPAN>
		  
		  
            <SELECT NAME="Status" ID="Status" TABINDEX="2" onChange="status_change()">
              <OPTION selected="selected" class="Choose">Choose...</OPTION>
			<%= statusoutput %>
            </SELECT>
		  
		  
		  <INPUT NAME="statusPH" TYPE="hidden" ID="statusPH">
            </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText"><span class="formLabel">Emergency: </span>
             
			  &nbsp;
              <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="Yes" />
              Yes  
              &nbsp; &nbsp; &nbsp;
			  <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="No" />
               
              No 
            <BR />            
	    </TD>
          <TD  bgcolor="#D2FFD2" CLASS="SearchFormText">
			<P><span class="formLabel">Given To</span>
			  <% if fromdispatch<>"yes" then %>
			  	<INPUT NAME="GivenTo" TYPE="text" disabled="disabled" ID="GivenTo" TABINDEX="4"/>
			  <% else %>
			  	
			  	<INPUT NAME="GivenTo" TYPE="text" SIZE="20" ID="GivenTo" TABINDEX="4" />									
			  	<BR/>
			  	<span class="formLabel">Dispatched Time </span>
			  	<INPUT NAME="GivenToTime" TYPE="text" SIZE="20" ID="GivenToTime" />
				<BR/>
				<span class="formLabel">Follow Up Time</span>
				<INPUT NAME="FollowUp" TYPE="text" SIZE="20" ID="FollowUp" />
		      <% end if %>
		    </P>
         </TD>
        </TR> 
        <TR>
          <TD COLSPAN="2" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText"><span class="formLabel">Notes</span> 
             
            <TEXTAREA NAME="Notes" COLS="70" ROWS="5" ID="Notes"></TEXTAREA>
             </TD>
          </TR>
        <TR>
			<TD HEIGHT="10">&nbsp;</TD>
			<TD HEIGHT="10" ALIGN="right">
				<INPUT TYPE="image" NAME="Save" SRC="Images/SaveButton.png" ALT="save" WIDTH="60" HEIGHT="30" BORDER="0"  onclick="FrmSubmit()"/> 
			</TD>
        </TR>
        <tr>
			<td colspan="2" class="SearchFormText">
				Call History:
			</td>
        </tr>
		<%= notesoutput %>
		
<% if ticketnum<>"" then %>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ra(1,0) %>"
		document.getElementById("Contract_ID").value="<%= ra(1,0) %>"
		document.getElementById("Ticket_ID_Display").value="<%= ra(0,0) %>"
		document.getElementById("Ticket_ID").value="<%= ra(0,0) %>"
		document.getElementById("CallNumber").value="<%= rowcount+1 %>"
		<% if request.QueryString("FromDispatch")<>"yes" then %> 
			document.getElementById("GivenTo").value="<%= ra(3,0) & " : " & ra(9,0)%>"
		<% else %>
			document.getElementById("GivenTo").value="<%= ra(3,0) %>"
			document.getElementById("GivenToTime").value="<%= ra(9,0) %>"
			document.getElementById("FollowUp").value="<%= ra(8,0) %>"
		<% end if %>
		 <% 'appliance list with selection based on chosen appliance
			x=0
			appliance=split(appliances,"|")
			do while x<=ubound(appliance)
				if ra(6,0)=appliance(x) then
					%>
					document.getElementById("Appliance").selectedIndex=<%= x  %>
					<%
					exit do
				end if
				x=x+1
			loop
							
				
			'------------- set status to ticket status
			x=0
			statuses=stats
			statarray=split(statuses,"|")
			do while x<=ubound(statarray)
				if ra(2,0)=statarray(x) then
					%>
					document.getElementById("Status").selectedIndex=<%= x+1 %>
					document.getElementById("statusPH").value= "<%= x+1 %>"
					<%
					
					exit do
				end if
				x=x+1
			loop
			
			
			'------------- emergency radio buttons
			%>
			
			<%
			if ra(7,0)="Yes" then
				%>
				document.TicketForm.Emergency[0].checked=true
				<%
			end if
			
			if ra(7,0)="No" then
				%>
				document.TicketForm.Emergency[1].checked=true
				<%
			end if
					  
		%>
	</SCRIPT>
<%
end if 'the if that checks if ticketnum<>""

ContractId=request.QueryString("Contract_ID")
if ContractID<>"" then
	%>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ContractId %>"
		document.getElementById("Contract_ID").value="<%= ContractId %>"
		document.getElementById("CallNumber").value=1
	</SCRIPT>

	<%
end if 'for if contractid<>""

if isobject("trs") then 
	if trs.state=1 then trs.close
end if

set trs=nothing

if rs.state=1 then rs.close
set rs=nothing

db.close
set db=nothing
%>
	</TABLE>
            
    </TD>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerLeft.png"></TD>
    <TD WIDTH="560" HEIGHT="20" BACKGROUND="Images/TableLowerMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerRight.png"></TD>
  </TR>
</TABLE>
</FORM>
<script src="Content/Scripts/jquery.js"></script>
<script type="text/javascript">
	window.onload = function () {
		$("#Appliance").change(function () {
			var jThis = $(this);
			var jThisVal = jThis.val();

			if (jThisVal == "") return false;

			top.ScriptViewer.ScrollTo("#scriptPanel-Search", "");
			top.ScriptViewer.SearchForScripts(jThisVal, true, true, true);
		});
	};
</script>
</BODY>
</HTML>
